package com.content.management.biz.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.content.management.biz.ContentManageService;
import com.content.management.domain.dto.PublishContentDto;
import com.content.management.domain.entity.BusPublishContent;
import com.content.management.domain.entity.DictionaryData;
import com.content.management.domain.query.PublishContentQuery;
import com.content.management.domain.vo.PublishContentVo;
import com.content.management.service.BusPublishContentService;
import com.content.management.service.DictionaryDataService;
import com.content.management.util.DateHelper;
import com.content.management.util.enums.DirectoryFlag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * @author xinghu
 * @version 1.0
 * @date 2020/4/4 4:25 下午
 */
@Slf4j
@Service
@AllArgsConstructor
public class ContentManageServiceImpl implements ContentManageService {

	private final BusPublishContentService publishContentService;

	private final DictionaryDataService dictionaryDataService;

	@Override
	public IPage<PublishContentVo> voPage(PublishContentQuery query) {
		BusPublishContent content = new BusPublishContent();
		BeanUtils.copyProperties(query, content);
		IPage<BusPublishContent> page = publishContentService.lambdaQuery()
				.eq(Objects.nonNull(query.getPublishStatus()) && query.getPublishStatus().equals(2), BusPublishContent::getPublishStatus, query.getPublishStatus())
				.like(StringUtils.isNotBlank(query.getContentTopic()), BusPublishContent::getContentTopic, query.getContentTopic())
				.eq(StringUtils.isNotBlank(query.getClassification()), BusPublishContent::getClassification, query.getClassification())
				.ge(StringUtils.isNotBlank(query.getStartPublishTime()), BusPublishContent::getPublishTime, DateHelper.strToDateTime(query.getStartPublishTime()))
				.le(StringUtils.isNotBlank(query.getEndPublishTime()), BusPublishContent::getPublishTime, DateHelper.strToDateTime(query.getEndPublishTime()))
				.ge(StringUtils.isNotBlank(query.getStartCreateTime()), BusPublishContent::getCreateTime, DateHelper.strToDateTime(query.getStartCreateTime()))
				.le(StringUtils.isNotBlank(query.getEndCreateTime()), BusPublishContent::getCreateTime, DateHelper.strToDateTime(query.getEndCreateTime()))
				.eq(Objects.nonNull(query.getPublishType()), BusPublishContent::getPublishType, query.getPublishType())
				.page(new Page<>(query.getCurrent(), query.getSize()));
		Map<String, DictionaryData> mapData = dictionaryDataService.getMapByDirectoryFlag(DirectoryFlag.CLASSIFICATION_FLAG.getFlag());
		return page.convert(item -> PublishContentVo.of(item, mapData));
	}

	@Override
	public BusPublishContent getById(Long id) {
		return publishContentService.getById(id);
	}

	@Override
	public boolean deleteById(Long id) {
		return publishContentService.removeById(id);
	}

	@Override
	public boolean saveOrUpdateContent(PublishContentDto dto) {
		DictionaryData data = dictionaryDataService.lambdaQuery().eq(DictionaryData::getDictionaryFlag, dto.getClassification()).one();
		if (Objects.isNull(data)) {
			throw new IllegalArgumentException("无效的所属分类");
		}
		return publishContentService.saveOrUpdate(dto.to());
	}

	@Override
	public boolean withdrawById(Long id) {
		return publishContentService.lambdaUpdate()
				.set(BusPublishContent::getPublishStatus, 1)
				.eq(BusPublishContent::getId, id)
				.update();
	}


}
